<?php
class Game extends DataObject {
	static $db = array(
		"GameDate" => "Date",
	    "GameTime" => "Time",
	    "Round" => "Int",
	    "GoalsHomeTeam" => "Int",
	    "GoalsGuestTeam" => "Int",
	    "FPHome" => "Int",
	    "FPGuest" => "Int",
	);
	
	static $has_one = array(
	   "HomeTeam" => "Team",
	   "GuestTeam" => "Team",
	   "RefereeTeam" => "Team",
	   "League" => "League"
	);
    public function EditForm() {
    	
    	$datePicker = new CalendarPickerField('GameDate-'.$this->ID);
    	$datePicker->setValue($this->GameDate);
    	$teams = $this->League()->Teams();
    	$ds = array();
    	$ds[0] = '';
    	foreach ($teams AS $team) {
    		$ds[$team->ID] = $team->Title;
    	}
    	$teams = $this->League()->AllTeams();
    	$ads = array();$ads[0] = '';
        foreach ($teams AS $team) {
            $ads[$team->ID] = $team->Title;
        }
    	
    	$timeField = new TimeField('GameTime-'.$this->ID,'',substr($this->GameTime,0,5));
        $timeField->addExtraClass('time');
    	$selectHome = new DropdownField('HomeTeam-'.$this->ID,'',$ds,$this->HomeTeamID);
    	$selectGuest = new DropdownField('GuestTeam-'.$this->ID,'',$ds,$this->GuestTeamID);
    	$selectReferee = new DropdownField('RefereeTeam-'.$this->ID,'',$ads,$this->RefereeTeamID);
    	$goalsHome = new TextField('GoalsHome-'.$this->ID,'',$this->GoalsHomeTeam,2);
    	$goalsGuest = new TextField('GoalsGuest-'.$this->ID,'',$this->GoalsGuestTeam,2);
    	$fPHome = new TextField('FPHome-'.$this->ID,'',$this->FPHome,2);
    	$fPGuest = new TextField('FPGuest-'.$this->ID,'',$this->FPGuest,2);
    	$onlyThis = new CheckboxField('ChangeThis-'.$this->ID,'');
    	
    $ret ='
                <td>'.$datePicker->Field().'</td>
                <td>'.$timeField->Field().'</td>
                <td>'.$selectHome->Field().'</td>
                <td>'.$selectGuest->Field().'</td>
                <td>'.$selectReferee->Field().'</td>
                <td>'.$goalsHome->Field().'</td>
                <td>'.$goalsGuest->Field().'</td>
                <td>'.$fPHome->Field().'</td>
                <td>'.$fPGuest->Field().' ' .$onlyThis->Field().'</td>
                <td><input name="saveGame-'.$this->ID.'" type="image" src="cms/images/alert-good.gif" alt="save" /></td>
    ';
	return $ret;
    }
    
    public function Result() {
    	
    	return ($this->GoalsHomeTeam < 0 || $this->GoalsGuestTeam < 0)? '-- : --':$this->GoalsHomeTeam . ' : ' . $this->GoalsGuestTeam;
    	
    }
	
}